﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    { 
        public double TargetMTR = 0;

        /// <summary>
        /// 显示所有的布卷列表
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult MTWB06(string returnUrl)
        {
            ModelState.Clear();
            try
            {
                ViewBag.WBBS = WBBS;
                ViewBag.ReturnUrl = returnUrl;
            }
            catch (Exception ex)
            { 
                Tracer.Error(ex.StackTrace, ex);
            }      
            return PartialView();
        }
        public ActionResult MTWB06SP(string returnUrl)
        {
            ModelState.Clear();
            WBBSTModel mx = new WBBSTModel();            
            ViewBag.WBBS = WBBS;
            try
            {
                if (TWBS == HWBS)
                    VHP.WBW.POSTCHG();
                mx.REMARK = (TWBS - HWBS).ToString() + "-待入库:" + CPWBS.ToString();
                mx.WBBSTKNR = HWBS.ToString() + "-已入库";
            }
            catch (Exception ex)
            {
                mx.REMARK = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }            
            ViewBag.Model = mx;
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(mx);
        }
        /// <summary>
        /// 装载所有布卷
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult STWB06(string returnUrl)
        {

            ModelState.Clear();
            SLO = "01";
            SLB = "CP";
            LO = "01";
            LB = "CW";
            LP = "01      ";
            WBBSTModel mx = new WBBSTModel();
            mx.WHP = LO + LB + LP;
            mx.TERMNO = app.TERMNO;
            mx.STAFF = app.User;
            mx.REMARK = "请扫描";
            try
            {
                if (WBBS == null)
                {
                    TWBS = 0;
                    HWBS = 0;
                    WB2WH();
                    
                }
                if (WBBS == null)
                {
                    mx.REMARK = "没有可以扫描的布卷，请核对";
                    return PartialView(mx);
                }

                if (LMODEL != null)
                {
                    if (!LMODEL.IsDataValid)
                    {
                        VHP.WBW.POSTCHG();
                        ((WBBSTModel)LMODEL).WBBSTKNR = "";
                        return PartialView(LMODEL);
                    }
                }

                if (LMODEL != null)
                {
                    mx.REMARK = ((WBBSTModel)LMODEL).WBBSTKNR + LMODEL.REMARK;
                }

            }
            catch (Exception ex)
            {
                mx.REMARK = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return PartialView(mx);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult STWB06(WBBSTModel model, string returnUrl)
        {
            try
            {
                MOVE(model);//移库操做 
                LMODEL = model;
                ModelState.Clear();
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void M2WB(DataRow _dwWB)
        {
            foreach (WBBSTModel wb in WBBS)
            {
                if (wb.WBBSTKNR.Trim() == _dwWB["WBBSTKNR"].ToString().Trim())
                {
                    wb.LO = _dwWB["WBBLO"].ToString();
                    wb.LB = _dwWB["WBBLB"].ToString();
                    wb.LP = _dwWB["WBBLP"].ToString();
                    CPWBS = CPWBS.Replace(wb.PACKNR + ",", "");
                    HWBS++;
                    break;
                }
            }
        }
        public void WB2WH()
        {
            TargetMTR = 0;
            MessageInfo msg = new MessageInfo();
            if (VVTS == null)
                return;
            foreach (VVTLIEFModel vvt in VVTS)
            {
                msg.LIEFSCH = vvt.LIEFSCH;
                msg.AUFTYP = vvt.AUFTYP;
                msg.KRS = vvt.KRS;
                msg.LFD = vvt.LFD;
                msg.POS = vvt.POS;
                msg.LO = SLO;
                msg.LB = SLB;
                DataSet SResult = VHP.WBB.WB2WH(msg);
                if (WBBS == null)
                {
                    WBBS = new List<WBBSTModel>();
                }
                WBBS.Clear();
                WBLIST = SResult.Tables[0].Copy();
                foreach (DataRow row in WBLIST.Rows)
                {
                    WBBSTModel m = new WBBSTModel();
                    m.WBBSTKNR = row["WBBSTKNR"].ToString();
                    m.LO = row["WBBLO"].ToString();
                    m.LB = row["WBBLB"].ToString();
                    m.LP = row["WBBLP"].ToString();
                    m.WHP = m.LO + m.LB + m.LP;
                    m.QMTR = Convert.ToDouble(row["WBBISTKLG"].ToString()) / 1000;
                    m.QYD = Helper.RoundX(m.QMTR / 0.9144, 2);
                    m.REMARK = "";
                    m.PACKNR = row["WBBPACKNR"].ToString();
                    WBBS.Add(m);
                    CPWBS += m.PACKNR + ",";
                }
                TWBS = WBBS.Count;
            }
        }
        public void MOVE(WBBSTModel model)
        {
            if (model == null)
                return;
            if (WBBS == null)
                return;
            if (model.WBBSTKNR == null)
                return;
            if (model.WHP == null)
                return;
            string filter = "";
            DataRow[] rows = null;
            model.WHP = model.WHP.PadRight(12);
            model.WBBSTKNR = Helper.Left(model.WBBSTKNR.PadRight(12), 12);
            string _lo = model.WHP.Substring(0, 2);
            string _lb = model.WHP.Substring(2, 2);
            string _lp = model.WHP.Substring(4, 8);
            var result = WBBS.Where(p => p.WBBSTKNR == model.WBBSTKNR && p.LO == _lo && p.LB == _lb && p.LP == _lp).OrderByDescending(p => p.WBBSTKNR);
            if (result.Count() > 0)
            {
                model.IsDataValid = false;
                model.REMARK = "已经入库";
                return;
            }
            result = WBBS.Where(p => p.WBBSTKNR == model.WBBSTKNR).OrderByDescending(p => p.WBBSTKNR);
            if (result.Count() == 0)
            {
                model.IsDataValid = false;
                model.REMARK = "不在范围内";
                return;
            }
            
            model.IsDataValid = true;
            filter = "WBBSTKNR='" + model.WBBSTKNR + "'";
            rows = WBLIST.Select(filter);
            foreach (DataRow _dwWB in rows)
            {
                VHP.WBB.MOVE(_dwWB, _lo, _lb, _lp, RUNID);
                M2WB(_dwWB);
                model.REMARK = "完成(" + _dwWB["WBBPACKNR"].ToString() + ")";
            }
            //}
        }
        //[HttpPost]//UserEdit
        public ActionResult UserEdit(WBBSTModel model)
        {
            Response.Write(model.WBBSTKNR);
            Response.Write("<br />");
            Response.Write(model.QMTR);
            Response.Write("<br />");
            return View();
        }
        //[HttpPost]//UserEdit
        public ActionResult UserEdit_01(FormCollection form)
        {
            Response.Write(form["UserName"]);
            Response.Write("<br />");
            Response.Write(form[1]);
            Response.Write("<br />");
            return View();
        }
    }
}
